package com.snake.springboot.custom.service.impl.fileoss;
import com.alibaba.fastjson.JSONObject;
import com.snake.springboot.custom.api.model.fileoss.*;
import com.snake.springboot.custom.api.response.fileoss.OSSPoliciesRsp;
import com.snake.springboot.custom.api.service.fileoss.OSSService;
import com.snake.springboot.custom.service.config.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snake.common.security.MD5Util;
import org.snake.common.utils.HttpUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>本地OSS业务
 * 类{@code LocalOssImpl} </br>
 * 创建时间2025/5/8 10:00
 * @author yangxt
 * @see
 */
@Service
public class OSSLocalServiceImpl implements OSSService {
    private Logger logger = LoggerFactory.getLogger(OSSLocalServiceImpl.class);
    @Override
    public String ossType() {
        return OSSType.LOCAL_OSS.getCode();
    }
    @Override
    public OSSPoliciesRsp generatePolicies(String fileUrl, FileOSSModel ossModel) {
        String signature = signature(fileUrl, null, ossModel);
        OSSPoliciesRsp rsp = new OSSPoliciesRsp();
        rsp.setSignature(signature);
        rsp.setExpire("1");
        rsp.setPolicy("1");
        rsp.setHost(ossModel.getCurl()+Properties.localUploadFile);
        rsp.setDir(fileUrl);
        return rsp;
    }

    @Override
    @Async
    public void delOssUrls(OSSDelModel delModel, FileOSSModel ossModel) {
        String curl=ossModel.getCurl()+Properties.localDelFiles;
        //密钥过期时间
        DelFileModel model = new DelFileModel();
        model.setUrls(delModel.getImgUrls());
        model.setKey(ossModel.getAccessKeyId());
        model.setSignature(signature(ossModel.getAccessKeyId(),null,ossModel));
        String jsonString = JSONObject.toJSONString(model);
        try {
            logger.info("删除文件请求参数："+jsonString);
            String s = HttpUtils.doPost(curl, jsonString);
            logger.info("删除文件返回参数"+s);
        } catch (Exception e) {
            logger.info("删除文件失败",e);
        }
    }

    @Override
    public String generatePresignedUrl(OSSUrlModel urlModel, FileOSSModel ossModel) {
        return String.format("%s%s?signature=%s",ossModel.getCurl(),urlModel.getFileUrl(),signature(urlModel.getFileUrl(), urlModel.getTimeOut(),ossModel));
    }

    private String signature(String fileUrl, Long fileTimeOut, FileOSSModel ossModel){
        if (fileTimeOut==null||fileTimeOut<=0){
            fileTimeOut = Properties.fileTimeOut;
        }
        fileTimeOut = fileTimeOut*60000;
        String endTime = String.valueOf(System.currentTimeMillis() +fileTimeOut);
        String signature = MD5Util.signature(fileUrl, endTime, ossModel.getAccessKeyId(), ossModel.getAccessKeySecret());
        return signature;
    }

    @Override
    public String videoFirstFrame(OSSUrlModel urlModel, FileOSSModel ossModel) {
        return generatePresignedUrl(urlModel, ossModel)+"&"+Properties.xOssProcess;
    }
}
